/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

/**
 *
 * @author joep
 */

import com.bayu.services.dao.AvgStopTimeDao;
import com.bayu.services.dao.Graph2mlDao;
import com.bayu.services.entity.AvgStopTime;
import com.bayu.services.entity.Graph2ml;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/avg-sm60-update.htm")
public class AvgSm60Update {

     private AvgStopTimeDao dao;

    @Autowired
    public void setDao(AvgStopTimeDao dao) {
        this.dao = dao;

    }

    @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
        List list = this.dao.getById(Long.parseLong(id));
        map.addAttribute("data", list);
    }

     @RequestMapping(method=RequestMethod.POST)
    public String processForm(@ModelAttribute("AvgStopTime") AvgStopTime aStop,
            BindingResult result, ModelMap map, String date, HttpServletRequest request) {
        DateFormat format = new SimpleDateFormat("yy-MM-dd");
        Date tanggal;


        /*
        if (result.hasFieldErrors("tahun")) {
            try {
                tanggal = format.parse(date);
                aStop.setDate(tanggal);
            } catch (Exception e) {
            }
        }*/

        String id = request.getParameter("id");

        if (id != null) {
            aStop.setId(Long.parseLong(id));
            this.dao.update(aStop);
            return "redirect:view-avg-sm60-data.htm?msg=Data has been updated";
        } else {
            return "redirect:avg-sm60-update.htm";
        }

    }

}
